<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- cut | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- cut</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cut命令用来显示行中的指定部分，删除文件中指定字段。cut经常用来显示文件的内容，类似于下的type命令。</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1. 命令格式"></a>1. 命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut [参数] [file]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2. 命令功能"></a>2. 命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不指定 File 参数，cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3. 命令参数"></a>3. 命令参数</h2><ul>
<li>-b：仅显示行中指定直接范围的内容；</li>
<li>-c：仅显示行中指定范围的字符；</li>
<li>-d：指定字段的分隔符，默认的字段分隔符为“TAB”；</li>
<li>-f：显示指定字段的内容；</li>
<li>-n：与“-b”选项连用，不分割多字节字符；</li>
<li>–complement：补足被选择的字节、字符或字段； </li>
<li>–out-delimiter=&lt;字段分隔符&gt;：指定输出内容是的字段分割符；</li>
<li>–help：显示指令的帮助信息</li>
<li>–version：显示指令的版本信息。</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4. 使用实例"></a>4. 使用实例</h2><h3 id="实例1：截取文件其中一个字符"><a href="#实例1：截取文件其中一个字符" class="headerlink" title="实例1：截取文件其中一个字符"></a>实例1：截取文件其中一个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c2 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c2 /etc/passwd</span></div><div class="line">o</div><div class="line">i</div><div class="line">a</div><div class="line">d</div><div class="line">p</div><div class="line">y</div><div class="line">h</div><div class="line">a</div><div class="line">a</div><div class="line">p</div><div class="line">a</div><div class="line">t</div><div class="line">o</div><div class="line">y</div><div class="line">y</div><div class="line">b</div><div class="line">o</div><div class="line">s</div><div class="line">o</div><div class="line">s</div><div class="line">h</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提取第2个字符</p>
<h3 id="实例2：截取文件多个字符"><a href="#实例2：截取文件多个字符" class="headerlink" title="实例2：截取文件多个字符"></a>实例2：截取文件多个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c2-5,10 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c2-5,10 /etc/passwd</span></div><div class="line">oot:0</div><div class="line"><span class="keyword">in</span>:x:</div><div class="line">aemo2</div><div class="line">dm:x:</div><div class="line">p:x:l</div><div class="line">ync:0</div><div class="line">hutdx</div><div class="line">alt:0</div><div class="line">ail:1</div><div class="line">perax</div><div class="line">ames2</div><div class="line">tp:x5</div><div class="line">obod9</div><div class="line">ysteu</div><div class="line">ystee</div><div class="line">bus::</div><div class="line">olki:</div><div class="line">ss:x5</div><div class="line">ostf:</div><div class="line">shd::</div><div class="line">hron9</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;提取第2、第3、第4、第5和第10个字符</p>
<h3 id="实例3：提取前4个字符"><a href="#实例3：提取前4个字符" class="headerlink" title="实例3：提取前4个字符"></a>实例3：提取前4个字符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c-4 /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c-4 /etc/passwd</span></div><div class="line">root</div><div class="line">bin:</div><div class="line">daem</div><div class="line">adm:</div><div class="line">lp:x</div><div class="line">sync</div><div class="line">shut</div><div class="line">halt</div><div class="line">mail</div><div class="line">oper</div><div class="line">game</div><div class="line">ftp:</div><div class="line">nobo</div><div class="line">syst</div><div class="line">syst</div><div class="line">dbus</div><div class="line">polk</div><div class="line">tss:</div><div class="line">post</div><div class="line">sshd</div><div class="line">chro</div></pre></td></tr></table></figure>
<h3 id="实例4：打印从第10个字符开始到结尾"><a href="#实例4：打印从第10个字符开始到结尾" class="headerlink" title="实例4：打印从第10个字符开始到结尾"></a>实例4：打印从第10个字符开始到结尾</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -c10- /etc/passwd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cut -c5- /etc/passwd</span></div><div class="line">:x:0:0:root:/root:/bin/bash</div><div class="line">x:1:1:bin:/bin:/sbin/nologin</div><div class="line">on:x:2:2:daemon:/sbin:/sbin/nologin</div><div class="line">x:3:4:adm:/var/adm:/sbin/nologin</div><div class="line">:4:7:lp:/var/spool/lpd:/sbin/nologin</div><div class="line">:x:5:0:sync:/sbin:/bin/sync</div><div class="line">down:x:6:0:shutdown:/sbin:/sbin/shutdown</div><div class="line">:x:7:0:halt:/sbin:/sbin/halt</div><div class="line">:x:8:12:mail:/var/spool/mail:/sbin/nologin</div><div class="line">ator:x:11:0:operator:/root:/sbin/nologin</div><div class="line">s:x:12:100:games:/usr/games:/sbin/nologin</div><div class="line">x:14:50:FTP User:/var/ftp:/sbin/nologin</div><div class="line">dy:x:99:99:Nobody:/:/sbin/nologin</div><div class="line">emd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin</div><div class="line">emd-network:x:192:192:systemd Network Management:/:/sbin/nologin</div><div class="line">:x:81:81:System message bus:/:/sbin/nologin</div><div class="line">itd:x:998:996:User <span class="keyword">for</span> polkitd:/:/sbin/nologin</div><div class="line">x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin</div><div class="line">fix:x:89:89::/var/spool/postfix:/sbin/nologin</div><div class="line">:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin</div><div class="line">ny:x:997:995::/var/lib/chrony:/sbin/nologin</div></pre></td></tr></table></figure>
<h3 id="实例5：用-f-提取指定字段，-d-指定分隔符"><a href="#实例5：用-f-提取指定字段，-d-指定分隔符" class="headerlink" title="实例5：用 -f 提取指定字段，-d 指定分隔符"></a>实例5：用 -f 提取指定字段，-d 指定分隔符</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">cut -f1 -d<span class="string">" "</span> test.txt</div><div class="line">cut -f2,3 -d<span class="string">" "</span> test.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat test.txt</span></div><div class="line">No Name Mark Percent</div><div class="line">01 tom 69 91</div><div class="line">02 jack 71 87</div><div class="line">03 alex 68 98</div><div class="line">[root@localhost ~]<span class="comment"># cut -f1 -d" " test.txt</span></div><div class="line">No</div><div class="line">01</div><div class="line">02</div><div class="line">03</div><div class="line">[root@localhost ~]<span class="comment"># cut -f2,3 -d" " test.txt</span></div><div class="line">Name Mark</div><div class="line">tom 69</div><div class="line">jack 71</div><div class="line">alex 68</div></pre></td></tr></table></figure>
<h3 id="实例6：使用-–complement-提取指定字段之外的列"><a href="#实例6：使用-–complement-提取指定字段之外的列" class="headerlink" title="实例6：使用 –complement 提取指定字段之外的列"></a>实例6：使用 –complement 提取指定字段之外的列</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cut -f2 -d<span class="string">" "</span> --complement test.txt</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat test.txt                 </span></div><div class="line">No Name Mark Percent</div><div class="line">01 tom 69 91</div><div class="line">02 jack 71 87</div><div class="line">03 alex 68 98</div><div class="line">[root@localhost ~]<span class="comment"># cut -f2 -d" " --complement test.txt</span></div><div class="line">No Mark Percent</div><div class="line">01 69 91</div><div class="line">02 71 87</div><div class="line">03 68 98</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/64. Linux 命令- cut/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
